$(document).keydown(function (e) {
    var keycode = e.keyCode;
    if (keycode == 13) {
        $("#btnSearch").click();
    }
});

$(document).delegate("#btnSearch", "click", function () {
    $("#table").bootstrapTable('refresh');
});


//功能列表===========================================================================================================


function initTable() {
    var url = "/manage/func/list";
    $('#table').bootstrapTable({
        url: url,
        striped: true,
        cache: false,
        pagination: false,
        toolbar: "#toolbar",
        treeView: true,
        treeId: "funcId",
        treeField: "funcName",
        clickToSelect: true,
        columns: [{
            checkbox: true,
            align: 'center',
        }, {
            field: 'funcName',
            title: '功能名称',
            width: "300px"
        }, {
            field: 'funcId',
            title: '功能 ID'
        }, {
            field: 'parentId',
            title: '父节点 ID'
        }, {
            field: 'funcUrl',
            title: '功能URL'
        }, {
            field: 'status',
            title: '状态',
            formatter: function (value, row, index) {
                if ("1" == value) {
                    return '<span class="label label-success" style="font-size:12px;">上线</span>';
                    return "上线";
                } else {
                    return '<span class="label label-default" style="font-size:12px;">下线</span>';
                    return "下线";
                }
            }
        }, {
            field: 'visible',
            title: '是否页面可见',
            formatter: function (value, row, index) {
                if ("1" == value) {
                    return '<span class="label label-success" style="font-size:12px;">可见</span>';
                    return "可见";
                } else {
                    return '<span class="label label-default" style="font-size:12px;">不可见</span>';
                    return "不可见";
                }
            }
        }, {
            field: 'funcOrder',
            title: '排序编号'
        }, {
            field: 'funcId',
            title: '操作',
            formatter: function (value, row) {
                var result = "";
                result += "<a href='javascript:Base.dialogIframe(\"功能查看\" ,\"/manage/func/view/@funcId\")' class='btn btn-xs'><span class='glyphicon glyphicon-info-sign'></span> 查看</a>";
                result += "<a href='javascript:Base.dialogIframe(\"功能编辑\" ,\"/manage/func/modify/@funcId\")' class='btn btn-xs'><span class='glyphicon glyphicon-pencil'></span> 编辑</a>";
                result += "<a href='javascript:funcDeleteDialog(\"@funcId\", \"@funcName\")' class='btn btn-xs text-danger'><span class='glyphicon glyphicon-remove'></span> 删除</a>";

                result = result.replace(/@funcId/g, value);
                result = result.replace(/@funcName/g, row.funcName);

                return result;
            }
        }],
        responseHandler: function (res) {
            return res.data.rows;
        },
        queryParams: function (params) {
            var searchkey = $.trim($("#searchkey").val());
            var status = $("#status").val();
            var temp = {
                pageSize: 10000,
                pageNum: 1,
                sort: params.sort,
                searchkey: searchkey,
                status: status
            };
            return temp;
        }
    });
}

function reflashTable() {
    $("#table").bootstrapTable('refresh');
}

function funcDeleteDialog(funcId, funcName) {
    BootstrapDialog.show({
        title: '删除确认',
        message: '请再次确认是否删除功能【' + funcName + '】',
        type: BootstrapDialog.TYPE_DANGER,
        buttons: [{
            label: '确定',
            cssClass: 'btn-danger',
            action: function (dialog) {
                funcDelete(funcId);
                dialog.close();
            }
        }, {
            label: '取消',
            action: function (dialog) {
                dialog.close();
            }
        }]
    });
}

function onlineBatchDialog() {
    var selections = $('#table').bootstrapTable('getSelections');
    if (selections.length == 0) {
        tiperror("请选择需要上线的功能！");
        return;
    }

    BootstrapDialog.show({
        title: '批量上线确认',
        message: '请再次确认是否进行批量上线操作？',
        type: BootstrapDialog.TYPE_DANGER,
        buttons: [{
            label: '确定',
            cssClass: 'btn-danger',
            action: function (dialog) {
                funcOnline();
                dialog.close();
            }
        }, {
            label: '取消',
            action: function (dialog) {
                dialog.close();
            }
        }]
    });
}

function offlineBatchDialog() {
    var selections = $('#table').bootstrapTable('getSelections');
    if (selections.length == 0) {
        tiperror("请选择需要下线的功能！");
        return;
    }

    BootstrapDialog.show({
        title: '批量下线确认',
        message: '请再次确认是否进行批量下线操作？',
        type: BootstrapDialog.TYPE_DANGER,
        buttons: [{
            label: '确定',
            cssClass: 'btn-danger',
            action: function (dialog) {
                funcOffline();
                dialog.close();
            }
        }, {
            label: '取消',
            action: function (dialog) {
                dialog.close();
            }
        }]
    });
}

function exportDialog() {
    BootstrapDialog.show({
        title: '导出确认',
        message: '请再次确认是否进行导出操作？',
        type: BootstrapDialog.TYPE_DANGER,
        buttons: [{
            label: '确定',
            cssClass: 'btn-danger',
            action: function (dialog) {
                var searchkey = $.trim($("#searchkey").val());
                var status = $("#status").val();
                var param = {
                    searchkey: searchkey,
                    status: status
                };
                Base.executeExport("/manage/func/export", param);
                dialog.close();
            }
        }, {
            label: '取消',
            action: function (dialog) {
                dialog.close();
            }
        }]
    });
}


//功能新增修改===========================================================================================================


function initFuncAddPage() {
    initBootstrapValidator(funcAdd);
}

function initFuncModifyPage() {
    initBootstrapValidator(funcModify, "modify");
}

function initBootstrapValidator(submitFunc, type) {
    var option = {
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            funcId: {
                message: '功能名验证失败',
                validators: {
                    notEmpty: {
                        message: '功能ID不能为空'
                    },
                    stringLength: {
                        min: 6,
                        max: 30,
                        message: '功能名长度必须在6到30之间'
                    },
                    threshold: 6, //有6字符以上才发送ajax请求
                    remote: { //{"valid",true}代表有效，{"valid",false}代表无效，
                        url: '/manage/func/checkexist',
                        message: '功能已存在',
                        delay: 2000,
                        type: 'POST'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_]+$/,
                        message: '功能名由数字字母下划线和.组成'
                    }
                }
            },
            funcName: {
                validators: {
                    notEmpty: {
                        message: '功能名称不能为空'
                    }
                }
            }
        }
    };

    if (type == "modify") {
        delete option.fields.funcId;
    }

    $('form').bootstrapValidator(option).on('success.form.bv', function (e) {
        e.preventDefault();
        submitFunc();
    });

    $('#resetBtn').click(function () {
        $('form').data('bootstrapValidator').resetForm(true);
    });
}


//ajax提交===========================================================================================================


function funcAdd() {
    var data = $('form').serializej();

    Base.ajax({
        "url": "/manage/func/add",
        "data": data,
        "success": function (data) {
            if (data.code && data.data == 1) {
                tipsuccess("功能新增成功！");
                if (top.bootstrapDialogIframe) {
                    top.bootstrapDialogIframe.close();
                }
                if (top.reflashTable) {
                    top.reflashTable();
                }
            } else {
                tiperror("功能新增失败！");
            }
        }
    });
}

function funcModify() {
    var data = $('form').serializej();

    Base.ajax({
        "url": "/manage/func/modify",
        "data": data,
        "success": function (data) {
            if (data.code && data.data == 1) {
                tipsuccess("功能修改成功！");
                if (top.bootstrapDialogIframe) {
                    top.bootstrapDialogIframe.close();
                }
                if (top.reflashTable) {
                    top.reflashTable();
                }
            } else {
                tiperror("功能修改失败！");
            }
        }
    });
}

function funcDelete(funcId) {
    Base.ajax({
        "url": "/manage/func/delete",
        "data": {
            "funcId": funcId
        },
        "success": function (data) {
            if (data.code && data.data == 1) {
                tipsuccess("功能删除成功！");
                if (top.bootstrapDialogIframe) {
                    top.bootstrapDialogIframe.close();
                }
                if (top.reflashTable) {
                    top.reflashTable();
                }
            } else {
                tiperror("功能删除失败！");
            }
        }
    });
}

function funcOnline() {
    var selections = $('#table').bootstrapTable('getSelections');
    if (selections.length == 0) {
        tiperror("请选择需要上线的功能！");
        return;
    }

    var funcIds = [];
    $.each(selections, function (id, item) {
        funcIds.push(item.funcId);
    });
    funcIds = funcIds.join(",");

    Base.ajax({
        "url": "/manage/func/online",
        "data": {
            "funcIds": funcIds
        },
        "success": function (data) {
            if (data.code && data.data == 1) {
                tipsuccess("批量上线成功！");
                reflashTable();
            } else {
                tiperror("批量上线失败！");
            }
        }
    });
}

function funcOffline() {
    var selections = $('#table').bootstrapTable('getSelections');
    if (selections.length == 0) {
        tiperror("请选择需要下线的功能！");
        return;
    }

    var funcIds = [];
    $.each(selections, function (id, item) {
        funcIds.push(item.funcId);
    });
    funcIds = funcIds.join(",");

    Base.ajax({
        "url": "/manage/func/offline",
        "data": {
            "funcIds": funcIds
        },
        "success": function (data) {
            if (data.code && data.data == 1) {
                tipsuccess("批量下线成功！");
                reflashTable();
            } else {
                tiperror("批量下线失败！");
            }
        }
    });
}
